package com.hepinghospital.user.ui.activity.userinfo;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseActivity;
import com.hepinghospital.user.ui.NewsActivity;
import com.rest.business.RestProxy;
import com.rest.observer.BaseObserver;
import com.rest.response.BannerListResponse;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import jz.joyoung.robot.util.SoftKeyboardUtil;
import jz.joyoung.robot.util.UtilString;

/**
 * Created by HP on 2020/2/20.
 */

public class SearchHelpActivity extends BaseActivity {

    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.edt_search)
    EditText edtSearch;
    @BindView(R.id.title)
    RelativeLayout title;
    @BindView(R.id.rv_help_detail_list)
    RecyclerView rvHelpDetailList;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    List<BannerListResponse.Records> list = new ArrayList<>();
    int diagType;
    int current = 1;
    String tital = "", helpCode = "", helpName = "";


    private HelpDetailAdapter helpDetailAdapter;


    @Override
    protected int getLayoutResource() {
        return R.layout.activity_search_help;
    }

    @Override
    protected void initViews() {
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rvHelpDetailList.setLayoutManager(manager);
        helpDetailAdapter = new HelpDetailAdapter();
        rvHelpDetailList.setAdapter(helpDetailAdapter);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(refreshlayout -> getHelpDetailList(helpCode, tital));
        refreshLayout.setOnLoadMoreListener(refreshlayout -> {
            current++;
            getHelpDetailList(helpCode, tital);
        });
        edtSearch.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                // 按下完成按钮，这里和上面imeOptions对应
                tital = edtSearch.getText().toString();
                getHelpDetailList(helpCode, tital);
                SoftKeyboardUtil.hideSoftKeyboard(SearchHelpActivity.this);
            }
            return false;
        });
    }

    @Override
    protected void initData() {
        helpCode = getIntent().getStringExtra("helpCode");
        helpName = getIntent().getStringExtra("helpName");
        if (!UtilString.isEmpty(helpName)) {
            //帮助大类名字不为空
            edtSearch.setHint(helpName);
        } else {
            edtSearch.setHint("请输入问题");
        }

        current = 1;
        getHelpDetailList(helpCode, tital);
    }



    /**
     * 帮助小类列表
     *
     * @time 2020/5/11 16:00
     */
    class HelpDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        @NotNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            Context context = parent.getContext();
            View view = LayoutInflater.from(context)
                    .inflate(R.layout.item_help_detail, parent, false);
            return new ViewHolder(view);

        }

        @Override
        public void onBindViewHolder(@NotNull RecyclerView.ViewHolder holder, final int position) {
            final ViewHolder myHolder = (ViewHolder) holder;

            myHolder.tvHelpDetailName.setText(list.get(position).title);

            myHolder.itemView.setOnClickListener(v -> {
                Intent intent = new Intent(mContext, NewsActivity.class);
                intent.putExtra("helpContent", list.get(position).content);
                intent.putExtra("helpName", helpName);
                startActivity(intent);

            });


        }


        @Override
        public int getItemCount() {
            return list.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.tv_help_detail_name)
            TextView tvHelpDetailName;

            ViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }

    private void getHelpDetailList(String type, String tital) {
        RestProxy.getInstance().getHelpDetailList(type, tital, new BaseObserver<BannerListResponse>() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onSuccess(BannerListResponse bannerListResponse) {
                try {
                    Log.e("问题个数---", bannerListResponse.records + "");
                    list.clear();
                    list.addAll(bannerListResponse.records);
                    helpDetailAdapter.notifyDataSetChanged();
                } catch (Exception e) {
                    // 捕获异常，如果要捕获Json数据异常，写catch(JSONException e)
                    e.printStackTrace();
                    //打印出来就Log.e(TAG,Log.getStackTraceString(e));
                }
                refreshLayout.finishRefresh(true);
            }

            @Override
            public void onError(Throwable e) {
                refreshLayout.finishRefresh(false);
            }
        });
    }
}
